#!/bin/sh

sudo find build/ -maxdepth 1 -not -name "physics-front" -not -name "build" -not -name "static" | xargs rm -rf
mkdir build
cp -rv src/* build/
cd build

sudo apt install -y git curl

## ubuntu seccomp
#sudo apt install -y libseccomp-dev
#
#which lrun
#if [ $? -ne 0 ]; then
#    git clone https://github.com/quark-zju/lrun.git
#    cd lrun
#    sudo apt install -y rake
#    sudo make install
#fi

which docker
if [ $? -ne 0 ]; then
    sudo apt-get update
    sudo apt-get install -y \
        apt-transport-https \
        ca-certificates \
        curl \
        software-properties-common
    curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
    sudo apt-key fingerprint 0EBFCD88
    sudo add-apt-repository \
       "deb [arch=amd64] https://download.docker.com/linux/ubuntu \
       $(lsb_release -cs) \
       stable"
    sudo apt-get update
    sudo apt-get install -y docker-ce
fi

which docker-compose
if [ $? -ne 0 ]; then
   sudo curl -L https://github.com/docker/compose/releases/download/1.21.0/docker-compose-$(uname -s)-$(uname -m) -o /usr/local/bin/docker-compose
   sudo chmod +x /usr/local/bin/docker-compose
fi

sudo docker volume create reform-code-exec

cd reform_flask
sudo docker build -t zsyh/reform-flask --network=host .
cd ..

cd test
sudo docker build -t zsyh/reform-flask-test --network=host .
cd ..

cd code_exec
sudo docker build -t zsyh/reform-code-exec --network=host .
cd ..

cd modbustcp
sudo docker build -t zsyh/reform-modbustcp --network=host .
cd ..

if [ ! -d physics-front ];then
      git clone https://github.com/ywtdzh/physics-front.git
fi
cd physics-front
git pull origin master | grep up-to-date
if [ $? -ne 0 ]; then
    sudo docker run -it --rm -v "$PWD":/work -w /work --net=host node:8 npm --loglevel=info install
    sudo docker run -it --rm -v "$PWD":/work -w /work --net=host node:8 npm --loglevel=info run build
    sudo rm -rf ../static
    sudo mv -T build ../static
fi
cd ..
